package utils
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import model.LongClickEvent;
	
	import mx.core.InteractionMode;
	import mx.managers.DragManager;
	
	import spark.components.DataGroup;
	import spark.components.IconItemRenderer;
	import spark.components.List;
	import spark.skins.spark.ScrollerSkin;
	
	/**
	 * A subclass of IconItemRenderer that allows the decorator object to
	 * be dragged and dropped within the List or into other Lists.
	 */
	public class OurDraggableIconItemRenderer extends IconItemRenderer
	{
		/** valeur par defaut de l'alpha */
		private static const DEFAULT_VALUE:Number = 1;
		
		/** The Scroller skin to use when in mouse interaction mode */
		private var dragModeScrollerSkin:* = NoScrollBarScrollerSkin;
		/** The skin that the Scroller had before being changed to the drag mode scroller skin */
		private var initialScrollerSkin:*;
		
        /** nouveau champ : drag actif ou non*/
        private var _dragPersoEnabled:Boolean = false;
        
		private var timer:Timer = null;
		private var currentTarget:*;
		private var ancienEvent:MouseEvent = null;
		
        /** getter */
        public function get dragPersoEnabled():Boolean{
            return _dragPersoEnabled;
        }
        /** setter */
        public function set dragPersoEnabled(value:Boolean):void{
            _dragPersoEnabled = value;
        }

		override public function initialize():void{
			super.initialize();
			init();
		}
		
		private function init():void{
			ListSerie.myRenderer = this;
            //ajouter seulement si le drag est activé
            if(_dragPersoEnabled){
    			this.addEventListener(MouseEvent.MOUSE_DOWN, startLongClick);
    			this.addEventListener(MouseEvent.MOUSE_UP, stopLongClick);
    			this.addEventListener(MouseEvent.MOUSE_MOVE, stopLongClickByMove);
            }
		}
		
		/**
		 * demarrer le timer
		 */
		private function startLongClick(event:MouseEvent):void{
			ancienEvent = MouseEvent(event.clone());
			timer = new Timer(70,10); 
			timer.addEventListener(TimerEvent.TIMER, timerTop);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
			currentTarget = event.target;
			timer.start();
		}
		/**
		 * à chaque top du timer
		 */
		private function timerTop(event:Event):void{
			if(currentTarget && currentTarget is TextField){
				currentTarget.alpha-=0.1;
			}
		}
		/**
		 * qd le timer est terminé
		 */
		private function timerComplete(event:Event):void{
			//remettre l'ancienne valeur
			if(currentTarget is TextField){
				currentTarget.alpha = DEFAULT_VALUE;
			}
			//arreter le timer
			timer.stop();
			timer = null;
			trace("timer terminé");//dispatch event ici pour clic long
			
			//enlever les listeners sur les clics
			this.removeEventListener(MouseEvent.MOUSE_DOWN, startLongClick);
			this.removeEventListener(MouseEvent.MOUSE_UP, stopLongClick);
			
			//déclencher un evenement de laché de souris
			this.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
			//passer en mode souris (pour ne pas que ca scroll lorsqu'on drag)
			var parentList:List = owner as List;
			parentList.setStyle('interactionMode', InteractionMode.MOUSE);
			//enlever les barres de scroll sur le coté
			initialScrollerSkin = parentList.scroller.getStyle('skinClass');
			parentList.scroller.setStyle('skinClass', dragModeScrollerSkin);
			//déclencher le meme evenement que le premier (clic sur l'item)
			this.dispatchEvent(ancienEvent);
			//remettre les listeners
			init();
		}
		
		/**
		 * qd on arrete de cliquer
		 */
		public function stopLongClick(event:Event):void{
			if(timer != null){
				trace("stop timer");
				//arreter le timer
				timer.stop();
				timer = null;
			}
			//remettre la valeur par defaut
			if(currentTarget is TextField)
				currentTarget.alpha = DEFAULT_VALUE;
			//repasser en mode touch
			var parentList:List = owner as List;
			trace("remise en TOUCH");
			parentList.setStyle('interactionMode', InteractionMode.TOUCH);
			//remettre les barres de scroll
			parentList.scroller.setStyle('skinClass', initialScrollerSkin);
		}
		
		private function stopLongClickByMove(event:MouseEvent):void{
			//si le bouton est appuyé, arreter le timer
			if(event.buttonDown && timer){
				trace("arret du timer par move");
				stopLongClick(null);
			}
		}
	}
}